<template>
	<view class="page-body">
		<!-- 页面内容开始 -->
		<!-- 表格搜索组件开始 -->
		<vk-data-table-query v-model="queryForm1.formData" :columns="queryForm1.columns" @search="search">
		</vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<!-- 自定义按钮区域开始 -->
		<view>
			<el-row>
				<el-button type="success" size="small" icon="el-icon-circle-plus-outline" @click="addBtn">添加</el-button>
			</el-row>
		</view>
		<!-- 自定义按钮区域结束 -->

		<!-- 表格组件开始 -->
		<vk-data-table ref="table1" :action="table1.action" :columns="table1.columns" :query-form-param="queryForm1"
			:right-btns="['detail_auto','update','delete','more']" :selection="true" :row-no="false" :pagination="true"
			:right-btns-more="table1.rightBtnsMore" @update="updateBtn" @delete="deleteBtn"
			@current-change="currentChange" @selection-change="selectionChange">
		</vk-data-table>
		<!-- 表格组件结束 -->

		<!-- 添加或编辑的弹窗开始 -->
		<view class="page-dialog">
			<vk-data-dialog v-model="form1.props.show" :title="form1.props.title" width="1000px" center
				:fullscreen="false" :close-on-click-modal="false">
				<edit v-if="form1.props.show" @success="form1.props.show = false;refresh();" :cid="cid"
					:action="form1.props.action" :form-type="form1.props.formType" :item="form1.data"></edit>
			</vk-data-dialog>
		</view>
		<!-- 添加或编辑的弹窗结束 -->

		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	import edit from "./edit.vue"
	var that; // 当前页面对象
	var vk = uni.vk; // vk实例
	var originalForms = {}; // 表单初始化数据

	var hasInit = false

	export default {
		props: ["cid", "teacher"],
		components: {
			edit
		},
		data() {
			// 页面数据变量
			return {

				// 页面是否请求中或加载中
				loading: false,
				// init请求返回的数据
				data: {

				},
				// 表格相关开始 -----------------------------------------------------------
				table1: {
					rightBtnsMore: [{
							title: '去开播',
							onClick: item => {
								let url = ''
								vk.callFunction({
									url: 'admin/video_course/live_video/sys/getGoRoomSign',
									title: '加载中...',
									data: {
										room_id: item.room_id,
										user_number: this.teacher.mobile,
										user_role: 1,
										user_name: this.teacher.nickname,
										user_avatar: this.teacher.avatar || ''
									},
									success(data) {
										console.log(data)
										url = data.data
										window.open(url)
									}
								})
								console.log(url)

								// Window.location=
							}
						},
						{
							title: '回放预览',
							onClick: item => {
								if (item.basicInfo.status != 100) {
									vk.toast('回放未生成', 'none');
									return
								}
								let url = ''
								vk.callFunction({
									url: 'admin/video_course/live_video/sys/getHfRoomSign',
									title: '加载中...',
									data: {
										room_id: item.room_id,
										user_number: this.teacher.mobile,
										user_role: 1,
										user_name: this.teacher.nickname,
										user_avatar: this.teacher.avatar || ''
									},
									success(data) {
										console.log(data)
										url = data.data
										window.open(url)
									}
								})
								console.log(url)
								// Window.location=
							}
						}
					],
					// 表格数据请求地址
					action: "admin/video_course/live_video/sys/getList",
					// 表格字段显示规则
					columns: [{
							key: "room_id",
							title: "房间号",
							type: "text",
							width: 180
						},
						{
							key: "title",
							title: "直播标题",
							type: "text",
							width: 180
						},
						{
							key: "chapter.title",
							title: "章节名",
							type: "text",
							width: 180
						},
						{
							key: "start_time",
							title: "上课时间",
							type: "time",
							width: 160,
							valueFormat: "yyyy-MM-dd hh:mm:ss"
						},

						{
							key: "end_time",
							title: "下课时间",
							type: "time",
							width: 160,
							valueFormat: "yyyy-MM-dd hh:mm:ss"
						},
						{
							key: "basicInfo",
							title: "回放状态",
							width: 160,
							type: "html",
							defaultValue: "未设置",
							formatter: function(val, row, column, index) {
								let str = ''
								let status = row.basicInfo.status
								console.log(status)
								if (status == 100) {
									str = `<text style='color: #04c877;'>回放已生成</text>`
								}
								if (status == 10 || status == 20) {
									str = `<text style='color: #e1a500;'>回放生成中</text>`
								}
								if (status == 0) {
									str = `<text style='color: #999;'>回放未生成</text>`
								}
								return str;
							}
						},
						{
							key: "status",
							title: "状态",
							type: "html",
							defaultValue: "未设置",
							formatter: function(val, row, column, index) {
								let now = Date.now()
								let str = ''
								if (now < row.start_time) {
									str = `<text style='color: #e1a500;'>未开始</text>`
								}
								if (now >= row.start_time && now <= row.end_time) {
									str = `<text style='color: #04c877;'>直播中</text>`
								}
								if (now > row.end_time) {
									str = `<text style='color: #999;'>已结束</text>`
								}
								return str;
							}
						},
					],
					// 多选框选中的值
					multipleSelection: [],
					// 当前高亮的记录
					selectItem: ""
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1: {
					// 查询表单数据源，可在此设置默认值
					formData: {

					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns: [{
							key: "course_id",
							title: "所属课程",
							type: "text",
							width: 160,
							mode: "=",
							hidden: true
						},
						{
							key: "title",
							title: "直播名",
							type: "text",
							width: 160,
							mode: "%%"
						},
					]
				},
				form1: {
					"data": {
						"course_id": null,
						"sort": 10
					},
					"props": {
						"action": "",
						"columns": [{
							"key": "course_id",
							"title": "所属课程",
							"type": "text",
							"placeholder": "请输入课程id",
							"clearable": true,
							"showLabel": true,
							"disabled": true
						}, {
							"key": "title",
							"title": "章节名称",
							"type": "text",
							"placeholder": "请输入章节名称",
							"clearable": true,
							"showLabel": true
						}, {
							"key": "sort",
							"title": "排序",
							"type": "number",
							"placeholder": "排序",
							"showLabel": true,
							"controls": true,
							"precision": 0,
							"step": 1,
							"min": 0,
							"tips": "升序排序"
						}],
						"rules": {
							"course_id": [{
								"required": true,
								"message": "所属课程不能为空",
								"trigger": "change"
							}],
							"title": [{
								"required": true,
								"message": "分类名称不能为空",
								"trigger": "change"
							}],
							"sort": [{
								"required": true,
								"message": "排序不能为空",
								"trigger": "change"
							}]
						},
						"formType": "",
						"show": false,
						"loading": false
					}
				},
				// 其他弹窗表单
				formDatas: {},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			if (hasInit)
				return
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		created(options = {}) {
			if (hasInit)
				return
			that = this;
			vk = that.vk;
			that.queryForm1.formData['course_id'] = that.cid
			that.form1.data['course_id'] = that.cid
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 表单重置
			resetForm() {
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search() {
				that.$refs.table1.search();
			},
			// 刷新
			refresh() {
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow() {
				return that.$refs.table1.getCurrentRow();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val) {
				that.table1.selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				that.table1.multipleSelection = list;
			},
			// 显示添加页面
			addBtn() {
				that.resetForm();
				that.form1.props.action = 'admin/video_course/live_video/sys/add';
				that.form1.props.formType = 'add';
				that.form1.props.title = '直播排课';
				that.form1.props.show = true;
			},
			// 显示修改页面
			updateBtn({
				item
			}) {
				that.form1.props.action = 'admin/video_course/live_video/sys/update';
				that.form1.props.formType = 'update';
				that.form1.props.title = '编辑排课';
				that.form1.props.show = true;
				that.form1.data = item;
			},
			// 删除按钮
			deleteBtn({
				item,
				deleteFn
			}) {
				deleteFn({
					action: "admin/video_course/live_video/sys/delete",
					data: {
						_id: item._id
					},
				});
			},
			// 监听 - 批量操作的按钮点击事件
			batchBtn(index) {
				switch (index) {
					case 1:
						vk.toast("批量操作按钮1");
						break;
					case 2:
						vk.toast("批量操作按钮2");
						break;
					default:
						break;
				}
			}
		},
		// 监听属性
		watch: {

		},
		// 过滤器
		filters: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {
		color: #8C939D;
	}
</style>
